<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Shop\DistGoodsSetRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;

// 分销商
class DistGodsSetController extends BaseController
{
  public function __construct(model $model)
  {
    $this->repoist = $model;
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('phone'))) {
      $where['where']['phone'] = $request->get('phone');
    }
    if (!empty($request->get('name'))) {
      $where['like'] = ['goods_name' => $request->get('name')];
    }
    if (!empty($request->get('number'))) {
      $where['like'] = ['name' => $request->get('number')];
    }
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    $where['where']['shop_id'] = !empty($request->user->shop_id) ? $request->user->shop_id : '';
    $data =  $this->repoist->index($where);
    // $data['supplier'] = $request->user->supplier;
    return $data;
  }
  public function store(Request $request)
  {
    $params = $request->all();
    if (empty($params['goods_id'])) {
      return $this->returnMsg(100, '请选择商品');
    }
    $goodsArr = explode('.', $params['goods_id']);
    // 将数据处理下
    $dist_shop_id = '';
    $dist_shop_name = '';
    if (!empty($goodsArr[0])) {
      $goods_id = $goodsArr[0];
    }
    if (!empty($goodsArr[1])) {
      $goods_name = $goodsArr[1];
    }
    if (isset($params['dist_type'])) {
      if ($params['dist_type'] >= 2 && empty($params['dist_all'])) {
        return $this->returnMsg(103, '请选择具体的代理');
      }
      if ($request['dist_type'] >= 2) {
        foreach ($params['dist_all'] as $key => $v) {
          $allDist = explode('.', $v);
          if (!empty($allDist)) {
            $dist_shop_id .= $allDist[0] . ',';
            $dist_shop_name .= $allDist[1] . ',';
          }
        }
      }
    }
    $params['goods_id'] = !empty($goods_id) ? $goods_id : '';
    $params['goods_name'] = !empty($goods_name) ? $goods_name : '';
    $params['dist_shop_id'] = !empty($dist_shop_id) ? $dist_shop_id : '';
    $params['dist_shop_name'] = !empty($dist_shop_name) ? $dist_shop_name : '';
    $params['shop_id'] = $params['user']['shop_id'];
    return $this->repoist->add($params);
  }
  public function destroy($id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id
    ];
    return $this->repoist->del($where);
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['id' => $request->get('id'), 'shop_id' => $request->user->shop_id];
    $params = $request->all();
    if (empty($params['goods_id'])) {
      return $this->returnMsg(100, '请选择商品');
    }
    $goodsArr = explode('.', $params['goods_id']);
    // 将数据处理下
    $dist_shop_id = '';
    $dist_shop_name = '';
    if (!empty($goodsArr[0])) {
      $goods_id = $goodsArr[0];
    }
    if (!empty($goodsArr[1])) {
      $goods_name = $goodsArr[1];
    }
    if (isset($params['dist_type'])) {
      if ($params['dist_type'] >= 2 && empty($params['dist_all'])) {
        return $this->returnMsg(103, '请选择具体的代理');
      }
      if ($request['dist_type'] >= 2) {
        foreach ($params['dist_all'] as $key => $v) {
          $allDist = explode('.', $v);
          if (!empty($allDist)) {
            $dist_shop_id .= $allDist[0] . ',';
            $dist_shop_name .= $allDist[1] . ',';
          }
        }
      }
    }
    $params['goods_id'] = !empty($goods_id) ? $goods_id : '';
    $params['goods_name'] = !empty($goods_name) ? $goods_name : '';
    $params['dist_shop_id'] = !empty($dist_shop_id) ? $dist_shop_id : '';
    $params['dist_shop_name'] = !empty($dist_shop_name) ? $dist_shop_name : '';
    $params['shop_id'] = $params['user']['shop_id'];
    return $this->repoist->edit($where, $params);
  }
  public function show(Request $request, $id)
  {
    $where = [
      'id' => $id
    ];
    $data =  $this->repoist->getOne($where);
    if (!empty($data['dist_shop_name'])) {
      $shopNameArr = explode(',', $data['dist_shop_name']);
      $shopIdArr = explode(',', $data['dist_shop_id']);
      foreach ($shopNameArr as $key => $v) {
        if (empty($v)) {
          continue;
        }
        $arrays[] = (!empty($shopIdArr[$key]) ? $shopIdArr[$key] : '') . '.' . $v;
      }
    }
    // var_dump($arrays, 11111111);
    $data['dist_all'] = !empty($arrays) ? $arrays : [];
    if (!empty($data['goods_id'])) {
      $data['goods_id'] = $data['goods_id'] . '.' . $data['goods_name'];
    }
    return $this->returnMsg(200, '查询成功', $data);
  }
}
